package com.modules.controller.sys;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.constant.CommonConstant;
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.Result;
import com.modules.dto.sys.req.RoleEntityReq;
import com.modules.entity.sys.RoleEntity;
import com.modules.service.sys.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;


/**
 * role控制层
 * @author yyq
 *
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController<RoleEntity>{
	
	@Resource
	private RoleService service;
	
	
	/**
	 * 分页查询
	 * @return
	 */
	@GetMapping("/select")
	public Result<?> select(RoleEntity entity){
		// 查询范围：改成xml查询和用户管理那里的查询一样（如果对项目/公司开放的情况下：公司可以自定义，这个一般都有）
		entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
		LambdaQueryWrapper<RoleEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.like(StrUtil.isNotBlank(entity.getRname()), RoleEntity::getRname, entity.getRname());
		queryWrapper.eq(entity.getOid() != null, RoleEntity::getOid, entity.getOid());
		queryWrapper.eq(StrUtil.isNotBlank(entity.getType()), RoleEntity::getType, entity.getType());
		queryWrapper.eq(entity.getState() != null, RoleEntity::getState, entity.getState());
		queryWrapper.ne(!getUser().getRid().equals(CommonConstant.SYS_RID), RoleEntity::getRid, CommonConstant.SYS_RID);
		IPage<RoleEntity> iPage = service.page(getPage(), queryWrapper);
		return Result.page(iPage.getRecords(), iPage.getTotal());
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	@PostMapping("/selectAll")
	public Result<?> selectAll(){
		List<RoleEntity> list = service.list(Wrappers.lambdaQuery());
		return Result.success(list);
	}

	/**
	 * 保存
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "保存")
	@PostMapping("/save")
	public Result<?> save(@Valid RoleEntity entity){
		entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
		service.saveOrUpdate(entity);
		return Result.success();
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "删除单个")
	@DeleteMapping("/delete")
	public Result<?> delete(RoleEntity entity){
		return service.delete(entity.getRid());
	}
	
	/**
	 * 修改状态
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "修改状态")
	@PostMapping("/updateState")
	public Result<?> updateState(RoleEntity entity){
		service.updateById(entity);
		return Result.success();
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "批量删除")
	@PostMapping("/deleteBatch")
	public Result<?> deleteBatch(String ids){
		service.removeByIds(Arrays.asList(ids.split(StrUtil.COMMA)));
		return Result.success();
	}
	
	/**
	 * 查询授权菜单
	 * @return
	 */
	@GetMapping("/selectRoleMenuTree")
	public Result<?> selectRoleMenuTree(Integer rid){
		return service.selectRoleMenuTree(rid);
	}
	
	/**
	 * 授权菜单
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "授权菜单")
	@PostMapping("/saveRoleMenu")
	public Result<?> menuPows(Integer rid, String[] mids){
		return service.saveRoleMenu(rid, mids, getUser().getLastOid());
	}


	/**
	 * 查询授权菜单按钮
	 * @return
	 */
	@GetMapping("/selectRoleMenuOperation")
	public Result<?> selectRoleMenuOperation(Integer rid, String code){
		return service.selectRoleMenuOperation(rid, code);
	}

	/**
	 * 授权菜单按钮
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "授权菜单按钮")
	@PostMapping("/saveRoleMenuOperation")
	public Result<?> saveRoleMenuOperation(@RequestBody RoleEntityReq entityReq){
		return service.saveRoleMenuOperation(entityReq.getRid(), entityReq.getList());
	}


}